


#include "gl_Color.h"


/// <summary>
/// rgb 888 转换成 rgb 565
/// </summary>
/// <param name="n888Color">rgb 888</param>
/// <returns>rgb 565</returns>
RGB565 RGB888ToRGB565(const  RGB888 n888Color)
{
    RGB565 n565Color = 0;
    // 获取RGB单色，并截取高位
    unsigned char cRed = (n888Color & RGB888_RED) >> 19;
    unsigned char cGreen = (n888Color & RGB888_GREEN) >> 10;
    unsigned char cBlue = (n888Color & RGB888_BLUE) >> 3;
    // 连接
    n565Color = (cRed << 11) + (cGreen << 5) + (cBlue << 0);
    return n565Color;
}

/// <summary>
/// rgb 565 转换成 rgb 888
/// </summary>
/// <param name="n565Color">rgb 565</param>
/// <returns>rgb 888</returns>
RGB888 RGB565ToRGB888(const RGB565 n565Color)
{
    RGB888  n888Color = 0;
    RGB888  cRed = (n565Color & RGB565_RED) << 8;
    RGB888  cGreen = (n565Color & RGB565_GREEN) << 5;
    RGB888  cBlue = (n565Color & RGB565_BLUE) << 3;

   // n888Color =cBlue  | cGreen |cRed ;
    n888Color = cRed | cGreen | cBlue;
    return n888Color;
}

/// <summary>
/// 颜色模型转换
/// </summary>
/// <param name="RGB">RGB形式颜色</param>
/// <param name="HSV">HSV形式颜色</param>
void RGB_to_HSV(RGBType* RGB, HSVType* HSV)
{
    float R = RGB->R, G = RGB->G, B = RGB->B, rgbmax, rgbmin,m;
    if (R > 1)R = 1;
    if (R < 0)R = 0;
    if (G > 1)G = 1;
    if (G < 0)G = 0;
    if (B > 1)B = 1;
    if (B < 0)B = 0;

    rgbmin = R > G ? G : R; // Min(R, G);
    rgbmin = rgbmin > B ? B : rgbmin; // Min(rgbmin, B);

    rgbmax = R > G ? R : G; // Max(R, G);
    rgbmax = rgbmax > B ? rgbmax : B; // Max(rgbmax, B);
    m = rgbmax - rgbmin;

    if (rgbmax == 0) {
        HSV->H = 0;
        HSV->S = 0;
        HSV->V = 0;
    }
    else if (rgbmax == rgbmin)
    {
        HSV->H = 0;
        HSV->S = 0;
        HSV->V = rgbmax;
    }
    else if (rgbmax == R) {
        HSV->H = ((G - B) / m) / 6;
        if (HSV->H < 0)HSV->H += 1;
        HSV->S = 1 - rgbmin / rgbmax;
        HSV->V = rgbmax;
    }
    else if (rgbmax == G) {
        HSV->H = ((B - R) / m) / 6 + 0.33333333f;
        HSV->S = 1 - rgbmin / rgbmax;
        HSV->V = rgbmax;
    }
    else if (rgbmax == B) {
        HSV->H = ((R - G) / m) / 6 + 0.66666666f;
        HSV->S = 1 - rgbmin / rgbmax;
        HSV->V = rgbmax;
    }

}

/// <summary>
/// 颜色模型转换
/// </summary>
/// <param name="HSV">HSV形式颜色</param>
/// <param name="RGB">RGB形式颜色</param>
void HSV_to_RGB(HSVType* HSV, RGBType* RGB)
{
    float h = HSV->H, s = HSV->S, v = HSV->V, p, q, t, f;
    int i;
    if (h > 1)h = h - (int)h;
    if (h < 0)h = (int)h - h;
    if (s > 1)s = 1;
    if (s < 0)s = 0;
    if (v > 1)v = 1;
    if (v < 0)v = 0;
    h = h * 6;
    i = (int)(h);
    f = h - i;
  // if (i % 2 == 0)f = 1 - f;
    p = v * (1.0f - s);
    q = v * (1.0f - s * f);
    t = v * (1.0f - s * (1.0f - f));
   // t = q;

    switch (i)
    {
    case 6: (RGB->R = v, RGB->G = t, RGB->B = p); break;
    case 0: (RGB->R = v, RGB->G = t, RGB->B = p); break;
    case 1: (RGB->R = q, RGB->G = v, RGB->B = p); break;
    case 2: (RGB->R = p, RGB->G = v, RGB->B = t); break;
    case 3: (RGB->R = p, RGB->G = q, RGB->B = v); break;
    case 4: (RGB->R = t, RGB->G = p, RGB->B = v); break;
    case 5: (RGB->R = v, RGB->G = p, RGB->B = q); break;
    }
}



/// <summary>
/// 颜色模型转换
/// </summary>
/// <param name="RGB">RGB形式颜色</param>
COLOR_RGB RGB_to_COLOR(RGBType* RGB)
{
#if (COLOR_rgb565 == 1)
    COLOR_RGB R, G, B;
    R = RGB->R * 32, G = RGB->G * 64, B = RGB->B * 32;   // 2**5 = 32 ,2**6 = 64
    return (R << 11 | G << 5 | B);
#else  
    COLOR_RGB R, G, B;
    R = RGB->R * 255, G = RGB->G * 255, B = RGB->B * 255;
    return (R << 16 | G << 8 | B);
#endif
}

/// <summary>
/// 颜色模型转换
/// </summary>
/// <param name="HSV">HSV形式颜色</param>
COLOR_RGB HSV_to_COLOR(HSVType* HSV)
{
    RGBType RGB;
    HSV_to_RGB(HSV, &RGB);
    return RGB_to_COLOR(&RGB);
}




